package com.zmh.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.zmh.model.ResultMsg;
import com.zmh.model.book.WebsiteInfo;
import com.zmh.service.book.WebsiteService;

/** 
 * @author zhoumh
 * @version 创建时间：2018年4月18日 下午9:08:10 
 * @Description 
*/
@RestController
@RequestMapping("/websiteManager")
public class WebsiteManagerController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private WebsiteService websiteService;
	
	/**
	 * 分页查询网站列表
	 * @param page 页码
	 * @param limit 条数
	 * @return: ResultMsg
	 * @author: zhoumh
	 * @version: 创建时间：2018年4月20日 下午4:51:19 
	 * @Description: 
	 */
	@RequestMapping(value = "/getWebsiteList")
	public ResultMsg getWebsiteList(
			@RequestParam(value="page", required=true) String page,
			@RequestParam(value="limit", required=true) String limit) {
		//获取分页
		List<WebsiteInfo> websites = websiteService.getWebsiteList(page, limit);
		return new ResultMsg("0", "正常", 10, websites);
	}
	/**
	 * 根据ID查询网站信息
	 * @param id 网站ID
	 * @return: WebsiteInfo
	 * @author: zhoumh
	 * @version: 创建时间：2018年4月20日 下午4:51:50 
	 * @Description:
	 */
	@RequestMapping(value = "/getWebsiteById")
	public ResultMsg getWebsiteInfoById(@RequestParam(value="id") String id) {
		WebsiteInfo websiteInfo = websiteService.getWebsiteInfoById(id);
		return new ResultMsg("0", "正常", 1, new ArrayList<>(Arrays.asList(websiteInfo)));
	}
	/**
	 * 增加网站信息
	 * @return: ResultMsg
	 * @author zhoumh
	 * @version 创建时间：2018年5月6日 下午4:53:44
	 * @Description:
	 */
	@RequestMapping(value = "/addWebsite",method = RequestMethod.POST,consumes = "application/json; charset=utf-8")
	public ResultMsg addWebsite(@RequestBody WebsiteInfo websiteInfo){
		System.out.println(JSONObject.toJSONString(websiteInfo));
		int result = websiteService.addWebsite(websiteInfo);
		return new ResultMsg("0","增加成功");
	}
	/**
	 * 更新网站信息
	 * @return: ResultMsg
	 * @author: zhoumh
	 * @version: 创建时间：2018年4月21日 下午3:44:43 
	 * @Description:
	 */
	@RequestMapping(value = "/updWebsiteById",method = RequestMethod.POST,consumes = "application/json; charset=utf-8")
	public ResultMsg updWebsiteById(@RequestBody WebsiteInfo websiteInfo){
		System.out.println(JSONObject.toJSONString(websiteInfo));
		int result = websiteService.updWebsiteById(websiteInfo);
		return new ResultMsg("0","更新成功");
	}
	/**
	 * 根据ID删除网站信息
	 * @param id 网站ID
	 * @author: zhoumh
	 * @version: 创建时间：2018年5月6日 下午7:26:00
	 * @Description:
	 */
	@RequestMapping(value = "/delWebsiteById")
	public ResultMsg delWebsiteById(@RequestParam(value="id") String id) {
		System.out.println("要删除的id: "+id);
		int result = websiteService.delWebsiteById(id);
		return new ResultMsg("0", "删除成功");
	}
	
}
